【GreatSQL搭建】

# 关闭防火墙
systemctl stop firewalld
systemctl disable firewalld

# 关闭selinux
setenforce 0
sed -i '/^SELINUX=/c'SELINUX=disabled /etc/selinux/config

# 关闭swap
swapoff -a

# 优化内核参数
echo "
vm.swappiness = 0
fs.file-max = 1000000
net.core.somaxconn = 32768
net.ipv4.tcp_syncookies = 0
vm.overcommit_memory = 1
#net.ipv4.tcp_tw_recycle = 0
" >> /etc/sysctl.conf

# 修改mysql用户使用资源上限
echo "
*           soft    nofile         65535
*           hard    nofile         65535
*           soft    stack          32768
*           hard    stack          32768
*           soft    nproc          65535
*           hard    nproc          65535
" >> /etc/security/limits.conf

# 刷新生效
sysctl -p

# 安装依赖
yum install -y perl libaio-devel numactl-devel numactl-libs net-tools openssl openssl-devel pkg-config

# 上传jemalloc的rpm包，执行安装
rpm -ivh jemalloc-5.3.0-1.el7.x86_64.rpm

# 添加动态库
echo ' /usr/local/jemalloc/lib/ ' > /etc/ld.so.conf.d/jemalloc.conf
ldconfig

# 查看jemalloc是否安装
ldconfig -p | grep jemalloc

# 添加系统文件/etc/sysconfig/mysql
echo -e "LD_PRELOAD=/usr/local/jemalloc/lib/libjemalloc.so\nTHP_SETTING=never" > /etc/sysconfig/mysql

# 下载greatsql安装包，这里部署的是 GreatSQL-8.0.32-26-Linux-glibc2.28-x86_64.tar.xz 版本
# 下载地址：https://gitee.com/GreatSQL/GreatSQL/releases/tag/GreatSQL-8.0.32-26

# 查看Linux系统glibc版本
ldd --version

# 解压安装包
tar -xf  GreatSQL-8.0.32-26-Linux-glibc2.28-x86_64.tar.xz -C /usr/local/

# 创建软连接
ln -s /usr/local/GreatSQL-8.0.32-26-Linux-glibc2.28-x86_64/ /usr/local/greatsql

# 添加全局变量 (注意变量问题，已使用了\)
echo "
# GREATSQL_HOME
export GREATSQL_HOME=/usr/local/greatsql
# PATH
export PATH=\$PATH:\$GREATSQL_HOME/bin
" >> /etc/profile

# 刷新全局变量
source /etc/profile

# 使用提供的my.cnf配置文件 (如果是按以上路径进行部署，则无需修改my.cnf内容)
cd /etc/
curl -O https://gitee.com/su-bushi/linux-script/raw/master/basic/greatsql/my.cnf

# 新建mysql用户
groupadd mysql && useradd -g mysql mysql -M -s /sbin/nologin

# 新建数据及日志目录
mkdir -p /data/greatsql/{data,logs,run}

# 新建初始化临时目录
mkdir -p /var/lib/mysql-files

# 修改所属用户
chown mysql:mysql -R /data/greatsql
chown mysql:mysql -R /var/lib/mysql-files
chown mysql:mysql -R /usr/local/GreatSQL-8.0.32-26-Linux-glibc2.28-x86_64/ /usr/local/greatsql/

# 赋权
chmod 700 -R /data/greatsql

# 添加依赖配置（8.0.32-25以上版本需配置）
echo "LD_LIBRARY_PATH=/usr/lib64" >> /etc/sysconfig/mysql
ldconfig

# 添加greatsql的systemd启动文件 (注意变量问题，已使用了\)
cat <<EOF | tee /usr/lib/systemd/system/greatsql.service
[Unit]
Description=GreatSQL Server
Documentation=man:mysqld(8)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
[Service]
# some limits
# file size
LimitFSIZE=infinity
# cpu time
LimitCPU=infinity
# virtual memory size
LimitAS=infinity
# open files
LimitNOFILE=65535
# processes/threads
LimitNPROC=65535
# locked memory
LimitMEMLOCK=infinity
# total threads (user+kernel)
TasksMax=infinity
TasksAccounting=false

User=mysql
Group=mysql
# 如果是GreatSQL 5.7版本，此处需要改成simple模式，否则可能服务启用异常
# 如果是GreatSQL 8.0版本则可以使用notify模式
#Type=simple
Type=notify
TimeoutSec=0
PermissionsStartOnly=true
ExecStartPre=/usr/local/greatsql/bin/mysqld_pre_systemd
ExecStart=/usr/local/greatsql/bin/mysqld \$MYSQLD_OPTS
LimitNOFILE = 10000
Restart=on-failure
RestartPreventExitStatus=1
Environment=MYSQLD_PARENT_PID=1
PrivateTmp=false
EOF

# 重载systemd
systemctl daemon-reload

# 启动greatsql服务
systemctl start greatsql

# 设置开机自启
systemctl enable greatsql.service

# 查看服务状态
systemctl status greatsql

# 查看初始化root密码
cat /data/greatsql/logs/error.log |grep "root@localhost"

# 连接数据库
mysql -uroot -p

# 修改root密码
alter user user() identified by 'Test@2024';

# 创建远程root管理用户
create user 'root'@'%' identified by 'Test@2024';

# 授权可访问数据库
grant all on *.* to 'root'@'%' with grant option;

# 刷新生效
flush privileges;

# 查看greatsql数据库信息
\s

# 创建新用户 (选做)
create user 'gtc_user'@'%' identified by 'Test@2024';

# 授权可访问数据库 (选做)
grant all privileges on *.* to 'gtc_user'@'%' with grant option;

# 刷新 (选做)
flush privileges;